package ui;

import java.awt.Color;
import java.awt.Dimension;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.swing.BoxLayout;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

import obj.Edge;

public class VehicleQueueReportPanel extends JPanel {

	private static final long serialVersionUID = -988439039323100020L;

	private static JTable table;
	private static DefaultTableModel model;

	private static Boolean isTableDrawn = Boolean.FALSE;

	public VehicleQueueReportPanel() {

		String colNames[] = { "No of Cycles", "Edge", "Vehicles in Queue" };
		model = new DefaultTableModel(colNames, 0);
		setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		table = new JTable(model);
		this.add(table);
		table.setGridColor(Color.LIGHT_GRAY);
		table.setBackground(Color.WHITE);
		table.setBorder(null);
		table.setPreferredScrollableViewportSize(new Dimension(650, 170));
		table.setFillsViewportHeight(true);
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBorder(null);
		this.add(scrollPane);
	}

	static Map<String, Edge> edgeMap = new HashMap<String, Edge>();

	public static void drawTable(Map<Edge, Set<String>> carsInEdgesMap) {
		if (!isTableDrawn) {
			for (Edge e : carsInEdgesMap.keySet()) {
				model.addRow(new Object[] { "", e.getName(), "" });
				edgeMap.put(e.getName(), e);
				
				isTableDrawn = Boolean.TRUE;
			}
		}

	}

	public static void refreshTable(Map<Edge, Set<String>> carsInEdgesMap, int lightCycle) {
		int rows = table.getRowCount();
		int cols = table.getColumnCount();

		if (carsInEdgesMap != null) {

			for (int i = 0; i < rows; i++) {
				Edge e = edgeMap.get(table.getModel().getValueAt(i, 1));
				Set<String> vehicleSet = carsInEdgesMap.get(e);

				table.getModel().setValueAt(lightCycle, i, 0);
				table.getModel().setValueAt(vehicleSet.size(), i, 2);
			}
		}

	}
}
